// pages/articles/detail/detail.js

const app = getApp();
const model = require('../../../utils/model');
const util = require('../../../utils/util');

function formatDate(list){
  for(var i = 0; i < list.length; i ++) {
    list[i]['createtime'] = util.formatShortTime(new Date(1000*parseInt(list[i]['create_time'])));
  }
}

Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:0,
    detail: {},
    list: [],
    audioList:[],
    audioIndex:0,
    scrollHeight: 0,
    lastId: 0,
    canLoadMore: true,
    loadtips: '数据加载中...',
    showLoading:false
  },
  getDetail: function () {
    var me = this;
    var id = this.data.id;
    model.request({
      action: 'mwpdDetail',
      data: {
        id: id
      },
      success: function (res) {
        //console.log(res.data.flash_list);
        res.data.detail['createtime'] = util.formatShortTime(new Date(1000*parseInt(res.data.detail['createtime'])));
        me.setData({
          detail: res.data.detail,
          audioList:[res.data.detail]
        });
      }
    });
  },

  getList: function () {
    if (!this.data.canLoadMore){
      return;
    }
    this.data.canLoadMore = false;
    var me = this;
    var id = this.data.id;
    model.request({
      action: 'mwpdReadingList',
      data: {
        id:id,
        last_id: me.data.lastId
      },
      success: function (res) {
        me.data.canLoadMore = true;
  
        if (res.data.list && res.data.list.length) {
          formatDate(res.data.list);
          var newList = me.data.list.concat(res.data.list);
          var newAudioList = me.data.audioList.concat(res.data.list);
          me.setData({
            audioList:newAudioList,
            list: newList,
            lastId: res.data['last_id'],
            showLoading:false
          });
        }
        else {
          me.setData({
            showLoading:false
          });
          wx.showToast({
              title: '暂无更多数据',
              icon: 'loading',
              duration: 1000
          });
        }
      }
    });
  },
  
  scroll:function () {
    this.setData({
      showLoading:true
    });
  },

  lower: function () {
    this.getList();
  },

  play: function (event) {
    var index = event.target.dataset.index;

    var player = this.selectComponent('#player');
    console.log(index)
    console.log(player)
    player.play(index);
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    this.setData({
      id:options.id,
      scrollHeight: app.globalData.winHeight - 93
    });
    this.getDetail();
    this.getList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})